//
//  MYPublishImageTagImageCell.m
//  MuYing
//
//  Created by tbb on 2019/8/16.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYPublishImageTagImageCell.h"
#import "TBBTagImageView.h"
@implementation MYPublishImageTagImageCell
-(TBBTagImageView *)imageView {
    if (!_imageView) {
        _imageView = [[TBBTagImageView alloc] init];
        _imageView.userInteractionEnabled = YES;
    }
    return _imageView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.contentView addSubview:self.imageView];
}
-(void)setImage:(UIImage *)image {
    _image = image;
    [self layoutImageView];
}

- (void)layoutImageView {
    CGRect imageFrame;
    if (self.image.size.width > self.bounds.size.width || self.image.size.height > self.bounds.size.height) {
        CGFloat imageRatio = self.image.size.width/self.image.size.height;
        CGFloat photoRatio = self.bounds.size.width/self.bounds.size.height;
        
        if (imageRatio > photoRatio) {
            imageFrame.size = CGSizeMake(self.bounds.size.width, self.bounds.size.width/self.image.size.width*self.image.size.height);
            imageFrame.origin.x = 0;
            imageFrame.origin.y = (self.bounds.size.height-imageFrame.size.height)/2.0;
        }
        else {
            imageFrame.size = CGSizeMake(self.bounds.size.height/self.image.size.height*self.image.size.width, self.bounds.size.height);
            imageFrame.origin.x = (self.bounds.size.width-imageFrame.size.width)/2.0;
            imageFrame.origin.y = 0;
        }
    }
    else {
        imageFrame.size = self.image.size;
        imageFrame.origin.x = (self.bounds.size.width-self.image.size.width)/2.0;
        imageFrame.origin.y = (self.bounds.size.height-self.image.size.height)/2.0;
    }
    self.imageView.frame = imageFrame;
    self.imageView.image = self.image;
}
@end
